package cn.edu.njtc.controller;

import cn.edu.njtc.common.R;
import cn.edu.njtc.entity.pojo.PatientAdverseReaction;
import cn.edu.njtc.entity.vo.PatientAdverseReactionVo;
import cn.edu.njtc.service.PatientAdverseReactionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Tag(name = "患者药物不良反应记录接口")
@RestController
@RequestMapping("/api/patient-reaction")
public class PatientAdverseReactionController {

    @Autowired
    private PatientAdverseReactionService reactionService;
    @Operation(summary = "根据用户ID获取药物不良反应记录")
    @GetMapping("/user/{userId}")
    public R<List<PatientAdverseReactionVo>> getReactionsByUserId(@PathVariable Integer userId) {
        List<PatientAdverseReactionVo> reactions = reactionService.getReactionsByUserId(userId);
        return R.ok(reactions);
    }
    @Operation(summary = "根据记录ID获取药物不良反应详情")
    @GetMapping("/{recordId}")
    public R<PatientAdverseReactionVo> getReactionById(@PathVariable Integer recordId) {
        PatientAdverseReactionVo reaction = reactionService.getReactionById(recordId);
        if (reaction == null) {
            return R.failed("记录不存在或已被删除");
        }
        return R.ok(reaction);
    }
    @Operation(summary = "添加药物不良反应记录")
    @PostMapping
    public R<?> addReaction(@RequestBody PatientAdverseReaction reaction) {
        boolean success = reactionService.addReaction(reaction);
        return success ? R.ok("添加成功") : R.failed("添加失败");
    }
    @Operation(summary = "更新药物不良反应记录")
    @PutMapping
    public R<?> updateReaction(@RequestBody PatientAdverseReaction reaction) {
        boolean success = reactionService.updateReaction(reaction);
        return success ? R.ok("更新成功") : R.failed("更新失败");
    }
    @Operation(summary = "删除药物不良反应记录")
    @DeleteMapping("/{recordId}")
    public R<?> deleteReaction(@PathVariable Integer recordId) {
        boolean success = reactionService.deleteReaction(recordId);
        return success ? R.ok("删除成功") : R.failed("删除失败");
    }
}